//
//  MPRStyleViewController.m
//  720PMPR
//
//  Created by lanou on 15/10/24.
//  Copyright © 2015年 青菜君. All rights reserved.
//

#import "MPRStyleViewController.h"
#import "MPRStyleViewCell.h"
#import "PKRequestManager.h"
#import "MPRStyleModel.h"
@interface MPRStyleViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MPRStyleViewController

- (NSMutableArray *)dataArray
{
    if (!_dataArray)
    {
        _dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
}

- (void)requestData
{
    [PKRequestManager requestWithType:GET urlString:@"http://api.gougouvideo.com:8888/channel.php?v=1.8.2" parDic:nil finish:^(NSData *data) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dict in array)
        {
            MPRStyleModel *model = [[MPRStyleModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataArray addObject:model];
        }
        [_mainTableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"解析失败");
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self requestData];
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 20, 35, 40, 30)];
    _titleLabel.text = @"分类";
    _titleLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_titleLabel];
    _flushButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _flushButton.frame = CGRectMake(SCREEN_WIDTH - 40, 35, 30, 30);
    [_flushButton setImage:[UIImage imageNamed:@"iconfont-yuanjuxing-2"] forState:(UIControlStateNormal)];
    [_flushButton setTitle:@"刷新" forState:(UIControlStateNormal)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    label.text = @"刷新";
    label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:11];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [_flushButton addSubview:label];
    [self.view addSubview:_flushButton];
    
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT - 120)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [_mainTableView registerClass:[MPRStyleViewCell class] forCellReuseIdentifier:@"MPRStyleViewCell"];
    [self.view addSubview:_mainTableView];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MPRStyleViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPRStyleViewCell" forIndexPath:indexPath];
    cell.styleMosel = self.dataArray[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
